/*
 * Copyright (c) 2011, Julien Lamy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of the project's author nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef ec9c59e3_f524_40cd_8ab5_6f5f4a4cc3f8
#define ec9c59e3_f524_40cd_8ab5_6f5f4a4cc3f8

#include <iosfwd>
#include <stdint.h>

#include "config.h"

/**
 * @brief Helper class to read the header of a chunk.
 */
class BABELPOD_EXPORT ChunkReader
{
public :

    /**
     * @brief Create a helper to read a chunk of given type.
     *
     * Throws if chunk is not of correct type.
     */
    ChunkReader(std::istream & stream, char const* wanted_type);

    /** @brief Return the header length (in bytes) of the chunk. */
    uint32_t header_length() const;

    /** @brief Return the total length (in bytes) or the number of children of the chunk. */
    uint32_t total_length() const;

    /** @brief Return the number of bytes that have been read. */
    unsigned long bytes_read() const;

    /** @brief Test if the whole header has been read. */
    bool done() const;

    /** @brief Read a value of given type from the header. */
    template<typename T>
    void read(T & value);

    /** @brief Read an array of given type from the header. */
    template<typename T>
    void read(T * value, unsigned int count);

    /** @brief Discard all data from the header that have not been read. */
    void ignore_rest_of_header();

private :
    std::istream & stream_;

    uint32_t header_length_;
    uint32_t total_length_;

    unsigned long bytes_read_;
    bool header_length_read_;
    bool done_;

    void read_(char* destination, unsigned int size);
};

#include "chunkreader.txx"

#endif // ec9c59e3_f524_40cd_8ab5_6f5f4a4cc3f8
